<?php
/* autor dichaofeng
 * date 2016 08 02
 * @parms key,auth,userid
 * 异步加载api  更改订单状态 返佣递归层级处理
 **/
class ApiController extends Yaf_Controller_Abstract
{
    public function init()
    {
        /**
         * 如果是Ajax请求, 则关闭HTML输出
         */
        $key=$_GET["key"];
        $auth=$_GET["auth"];
        if($key!='api'||$auth!="123456"){
            exit("hack is comming~");
        }

        if ($this->getRequest()->isXmlHttpRequest()) {
            Yaf_Dispatcher::getInstance()->disableView();
        }
    }
    /* autor kongxiangyu
     * date 2016 08 02
     * @parms key,auth,userid
     * 异步加载ajax 改变客户端关闭48小时后 订单状态
     **/
    function orderChangeStatusAction()
    {

        $orderid=$_GET["orderid"];
        if($orderid==""){
            exit("orderid 不存在~~");
        }

        //处理数据库
        //$orderid
        $this->ApiModel    = new ApiModel();
        $this->ApiModel->changeStatus($orderid);
        exit("success~");
    }

    /* autor dihcaofeng
     * date 2016 08 02
     * @parms key,auth,userid
     * 异步加载ajax 改变客户端关闭48小时后 订单状态
     **/
    function orderChangeDuilieStatusAction()
    {

        $orderid=$_GET["orderid"];
        if($orderid==""){
            exit("orderid 不存在~~");
        }

        //处理数据库
        //$orderid
        $this->ApiModel    = new ApiModel();
        $this->ApiModel->changeDuilieStatus($orderid);
        exit("success~");
    }

    /* autor dichaofeng
   * date 2016 08 04
   * @parms key,auth,userid
   * 异步加载ajax 购买成功 返佣金
   **/
    function fanyongAction()
    {
        $userid=isset($_GET["userid"])?$_GET["userid"]:"";//获取当前用户userid
        $total=isset($_GET["total"])?$_GET["total"]:"";//获取当前订单金额

        if($userid==""){
            exit("用户userid 不存在~~");
        }
        if($total==""){
            exit("金额total 不存在~~");
        }

        //处理数据库
        //$orderid
        $this->ApiModel    = new ApiModel();
        $userinfo=$this->ApiModel->getUserinfo($userid);
        print_r($userinfo);
        if(isset($userinfo['recommend_id'])){
            if($userinfo['recommend_id']){
                $this->doTuijian($userid,$total);
            }

        }
        // print_r($result);

        exit("success~");
    } /* autor dichaofeng
   * date 2016 08 04
   * @parms key,auth,userid
   * 异步加载ajax 购买成功 返佣金
   **/
    function goCartAction()
    {
        $userid=isset($_GET["userid"])?$_GET["userid"]:"";//获取当前用户userid


        if($userid==""){
            exit("用户userid 不存在~~");
        }

        //处理数据库
        //$orderid
        $this->ApiModel    = new ApiModel();
        $this->ApiModel->goCart($userid);
        exit("success~");
    }

//    static function pfanyong(){
//
//    }
    /* autor dichaofeng
    * date 2016 08 04
    * @parms key,auth,userid
    * 递归处理 购买成功 返佣金
    **/
    function doTuijian($userid,$total){
        static $tree=0;
        $tree=$tree+1;
       // print_r($tree);
        $ApiModel    = new ApiModel();
        $userinfo=$ApiModel->getUserinfo($userid);
        $p_userinfo=$ApiModel->getUserinfo($userinfo['recommend_id']);
       // print_r($p_userinfo);
        if(isset($userinfo['recommend_id'])){ //有上级推荐人
            if($userinfo['recommend_id']){
                //存在推荐人id
                $yongjin=0;//定义佣金折扣
                if($tree==1){//上级返佣


                    if($userinfo['level']==1){ //当前是400/地区会员
                        //计算佣金
                        //无佣金
                        $yongjin=0;
                    }elseif($userinfo['level']==2){ //当前是区县会员
                        //计算佣金
                        if($p_userinfo['level']==1){
                            $yongjin=0.1*$total;
                        }

                    }elseif($userinfo['level']==3){ //当前是普通会员
                        //计算佣金
                        $yongjin=0;
                        if($p_userinfo['level']==1){
                            $yongjin=0.2*$total;
                        }elseif($p_userinfo['level']==2){
                            $yongjin=0.1*$total;
                        }elseif($p_userinfo['level']==3){
                            $yongjin=0.1*$total;
                        }

                    }elseif($userinfo['level']==4){ //当前是非会员
                        //计算佣金
                        $yongjin=0;
                        if($p_userinfo['level']==1){
                            $yongjin=0.3*$total;
                        }elseif($p_userinfo['level']==2){
                            $yongjin=0.2*$total;
                        }elseif($p_userinfo['level']==3){
                            $yongjin=0.1*$total;
                        }elseif($p_userinfo['level']==4){
                            $yongjin=0.1*$total;
                        }
                    }
                    //1:更新该推荐人id的userinfo 账户余额+佣金折扣   2:生成收益记录
                    $result=$ApiModel->fanyongAdd($userinfo['recommend_id'],$yongjin);
                    $result=$ApiModel->fanyongRecord($userinfo['recommend_id'],$yongjin,$userinfo['level']);


                }else{
                    if($p_userinfo['level']==1){ //当前是400/地区会员
                        $yongjin=40;
                        if($tree==2){//顶层返佣
                            if($userinfo['level']==2){ //当前是区县会员
                                $yongjin=0.1*$total;
                            }elseif($userinfo['level']==3){ //当前是普通会员
                                $yongjin=0.2*$total;
                            }elseif($userinfo['level']==4){//当前是非会员
                                $yongjin=0.3*$total;
                            }
                            //直到查到地区400 总代理 的id
                            //1:更新该地区400 id的userinfo 账户余额   2:生成收益记录

                        }elseif($tree==3){//顶层返佣
                            if($userinfo['level']==3){ //当前是普通会员
                                $yongjin=0.2*$total;
                            }elseif($userinfo['level']==4){//当前是非会员
                                $yongjin=0.2*$total;
                            }
                            //直到查到地区400 总代理 的id
                            //1:更新该地区400 id的userinfo 账户余额   2:生成收益记录

                        }elseif($tree==4){//顶层返佣
                            if($userinfo['level']==3){ //当前是普通会员
                                $yongjin=0.1*$total;
                            }elseif($userinfo['level']==4){//当前是非会员
                                $yongjin=0.1*$total;
                            }
                            //直到查到地区400 总代理 的id
                            //1:更新该地区400 id的userinfo 账户余额   2:生成收益记录

                        }
                        $result=$ApiModel->fanyongAdd($userinfo['recommend_id'],$yongjin);
                        $result=$ApiModel->fanyongRecord($userinfo['recommend_id'],$yongjin,$userinfo['level']);
                    }

                }

               $this->doTuijian($userinfo['recommend_id'],$total,0);
            }else{
                //是地区400
            }
        }else{ //无上级推荐人

        }
    }



}